package com.yztc.orangeentertainment.ui.main.subscribefragment.starlist;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.yztc.orangeentertainment.bean.starlistbean.ListBean;
import com.yztc.orangeentertainment.bean.starlistbean.StarListBean;
import com.yztc.orangeentertainment.config.SubscribeUrlConfig;
import com.yztc.orangeentertainment.http.SubscribeRetrofitService;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;

/**
 * Created by My on 2016/10/7.
 */
public class StarListModle implements StarListContract.Modle {
    @Override
    public void getStarListAll(String type, Map<String, String> params, final OnLoadStarListListener loadStarListListener, Context context) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(SubscribeUrlConfig.PATH.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<StarListBean> call=retrofit.create(SubscribeRetrofitService.class).getStarListAll(type,params);
        call.enqueue(new Callback<StarListBean>() {
            @Override
            public void onResponse(Call<StarListBean> call, Response<StarListBean> response) {
                if (response.isSuccessful()){
                    List<ListBean> list=response.body().getData().getList();
                    loadStarListListener.onSuccess(list);
                }else {
                    Log.i("qwe","wrong");
                }
            }

            @Override
            public void onFailure(Call<StarListBean> call, Throwable t) {
                loadStarListListener.onFailure((Exception) t);
            }
        });
    }

    @Override
    public void getSearchStar(final String type, final Map<String, String> params, final Context context, final OnLoadStarListListener loadStarListListener) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(SubscribeUrlConfig.PATH.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<StarListBean> call=retrofit.create(SubscribeRetrofitService.class).getSubSearchAll(type,params);
        call.enqueue(new Callback<StarListBean>() {
            @Override
            public void onResponse(Call<StarListBean> call, Response<StarListBean> response) {
                if (response.isSuccessful()){
                    if ( response.body().getData()!=null) {
                        List<ListBean> list = response.body().getData().getStar();
                        loadStarListListener.onSuccess(list);
                    }else {
                        Toast.makeText(context,"没有找到数据",Toast.LENGTH_SHORT).show();
                    }

                }else {
                    Log.i("qwe","wrong");
                }
            }

            @Override
            public void onFailure(Call<StarListBean> call, Throwable t) {
                loadStarListListener.onFailure((Exception) t);
            }
        });
    }

    @Override
    public void getStarListFromDb(Subscriber<List<ListBean>> subscriber) {

    }
}
